(function(){
	var f, t, at, oldValue,M,N,B,G=0,C;
	var p = 0, c = 0, o;
	var mouseontable = false;

	// chenk browser type
	var ca=navigator.userAgent.toLowerCase();
	M=ca.indexOf("opera")!=-1;
	N=ca.indexOf("msie")!=-1&&!M

	function install(form, textBox, url) {
		f = form;
		t = textBox;
		C = url;
		oldValue = t.value;

		// bind events
		bindEvent(t,"blur",hiddenTable);
		//bindEvent(t,"beforedeactivate",ka);
		if(t.addEventListener){
			t.onkeypress=keydown;
			t.onkeyup=keyup
		}else{
			bindEvent(t,N?"keydown":"keypress",keydown);
			bindEvent(t,"keyup",keyup);
		}

		initcss();
		inittable();

		bindEvent(window,"resize",inittablepos);

		window.setTimeout(checkInput, 500);
	}

	function hiddenTable() {
		if (!mouseontable)
			toggleTableVisibility(false);
	}

	function keydown(e) {
		//alert('down');
		if (showingTable())
		{
			if (e.keyCode==27) // esc
			{
				toggleTableVisibility(false);
				oldValue = t.value = o;
				return false;
			}

			if (upkey(e)||downkey(e)) // up or down
			{
				if (upkey(e)) moveup();
				else if (downkey(e)) movedown();
				
				//e.cancelBubble=true;
				//e.returnValue=false;
				return false;
			}
		}

		return true;
	}

	function keyup(e) {
		//alert('up');
		if (upkey(e)||downkey(e))
		{
			//e.cancelBubble=true;
			//e.returnValue=false;
			return false;
		}
	}

	function moveup() {
		if (c == 0) return;

		var a = p - 1;

		if (a < 0) a = c + 1 + a;

		highlightTableRow(a);
		getValue();
	}

	function movedown() {
		if (c == 0) return;

		var a = p + 1;

		if (a > c) a = 0;

		highlightTableRow(a);
		getValue();
	}

	function initTable() {
		for (var i = 0; i < at.rows.length ; i++)
			at.rows[i].className = 'ac-a';
	}

	function highlightTableRow(n) {
		if (p == 0) o = t.value;

		initTable();

		if (n > 0)
			at.rows[n-1].className = 'ac-b';

		p = n;
	}

	function getValue() {
		if (p == 0) t.value = o;
		else if (p > 0 && p <= c) {
			t.value = at.rows[p-1].cells[0].innerHTML;
		}

		oldValue = t.value;
	}

	function showingTable() {
		return at.style.visibility!="hidden"?true:false;
	}

	function upkey(e) {
		return e.keyCode==38;
	}

	function downkey(e) {
		return e.keyCode==40;
	}

	function bindEvent(object, event, func) {
		var d="on"+event;
		if(object.addEventListener){object.addEventListener(event,func,false)}
		else if(object.attachEvent){object.attachEvent(d,func)}
		else{
			var e=object[d];
			object[d]=function(){
				var f=e.apply(this,arguments),h=func.apply(this,arguments);
				return f==undefined?h:(h==undefined?f:h&&f)
			}
		}
	}

	function checkInput() {
		if (!mouseontable && t.value != oldValue)
		{
			if (t.value == "")
			{
				toggleTableVisibility(false);
			}
			else
			{
				search();
			}

			o = oldValue = t.value;
		}

		window.setTimeout(checkInput, 500);
	}

	function toggleTableVisibility(b){
		if (b) inittablepos();

		if (!(b == true && G > 0))
			at.style.visibility = b?"visible":"hidden";
	}

	function search() {
		send(urlencode(t.value));
	}

	function getXmlHttpRequest() {
		var a=null;
		try{
			a=new ActiveXObject("Msxml2.XMLHTTP")
		}catch(b){
			try{
				a=new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(c){
				a=null
			}
		}
	
		if(!a&&typeof XMLHttpRequest!="undefined")a=new XMLHttpRequest;
		
		return a
	}

	function send(key) {
		if(B&&B.readyState!=0&&B.readyState!=4){B.abort()}
		if(B)B.onreadystatechange=function(){};
		
		B=getXmlHttpRequest();
		
		if(B){
			B.open("GET",C+"&q="+key,true);
			B.onreadystatechange=function(){
				if(B.readyState==4&&B.responseText){
					switch(B.status){
						case 403:
							G=1000;
							break;
						case 302:
						case 500:
						case 502:
						case 503:
							G++;
							break;
						case 200:
							var b=B.responseText;
							eval(b)
						default:
							G=0
					}
				}
			};
			B.send(null);
		}
	}

	function urlencode(a){
		if(window.encodeURIComponent)return encodeURIComponent(a);
		return escape(a);
	}

	function filldata(ar, count) {
		c = count;
		p = 0;
		while(at.rows.length>0)at.deleteRow(-1);
		for (var i = 0; i < count ; i++ )
		{
			var e=at.insertRow(-1);
			e.p = i+1;
			e.onmouseup=Aa;
			e.onmouseover=mouseover;
			e.onmouseout=Ca;
			e.className = 'ac-a';
			var f=document.createElement("td");
			f.className="ac-c";
			$(f,ar[i][0]);
			e.appendChild(f);
			var h=document.createElement("td");
			$(h,'约有'+ar[i][1]+'条结果');
			h.className="ac-d";
			e.appendChild(h)
		}

		if (c > 0) toggleTableVisibility(true);
		else toggleTableVisibility(false);
	}

	function $(a,b){
		a.appendChild(document.createTextNode(b))
	}

	function Aa() {
		getValue();
		t.focus();
		toggleTableVisibility(false);
	}

	function mouseover() {
		mouseontable = true;
		highlightTableRow(this.p);
	}

	function Ca() {
		mouseontable = false;
	}
	
	function initcss() {
		var a=document.body.dir=="rtl",b=a?"right":"left",c=a?"left":"right",d=document.getElementsByTagName("head")[0],e=document.createElement("style"),f=null,h=null,m=false;
		if(document.styleSheets){
			d.appendChild(e);
			m=true;
			f=e.sheet?e.sheet:e.styleSheet
		}
		if(!f){
			h=document.createTextNode("");
			e.appendChild(h)
		}
		
		var k=function(D,E){
			var ma=D+" { "+E+" }";
			if(f){
				if(f.insertRule){
					f.insertRule(ma,f.cssRules.length)
				}else
					if(f.addRule){f.addRule(D,E)}
			}
			else{h.data+=ma+"\n"}
		};

		k(".ac-tb", "font-size:13px;font-family:arial,sans-serif;cursor:default;line-height:17px;border:1px solid black;z-index:99;position:absolute;background-color:white;margin:0;");
		k(".ac-a", "background-color:white;");
		k(".ac-b", "background-color:#36c;color:white;");
		k(".ac-c", "white-space:nowrap;overflow:hidden;text-align:"+b+";padding-"+b+":3px;"+(N||M?"padding-bottom:1px;":""));
		k(".ac-d", "white-space:nowrap;overflow:hidden;font-size:10px;text-align:"+c+";color:green;padding-"+b+":3px;padding-"+c+":3px;}");

		if(!m)d.appendChild(e)
	}

	function inittable() {
		at=document.createElement("table");
		at.id="completeTable";
		at.cellSpacing=(at.cellPadding="0");
		at.className="ac-tb";
		toggleTableVisibility(false);
		f.appendChild(at);
	}

	function inittablepos(){
		if(at){
			var w = at.style;
			w.left=ua(t,"offsetLeft")+"px";
			w.top=ua(t,"offsetTop")+t.offsetHeight-1+"px";
			w.width=t.offsetWidth+"px";
		}
	}

	function ua(a,b){
		var c=0;
		while(a){
			c+=a[b];
			a=a.offsetParent
		}
			
		return c
	}

	window.ac = {install:install, filldata:filldata};
})()